using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using System.Threading.Tasks;
using System;

namespace Acme.BookStore.Car
{
    public class OrdersAppService : CrudAppService<
        Orders,
        OrdersDto,
        int,
        PagedAndSortedResultRequestDto,
        CreateUpdateOrdersDto>,
        IOrdersAppService
    {
        private readonly IFleetcarAppService _fleetcarAppService;
        public OrdersAppService(IRepository<Orders, int> repository, IFleetcarAppService fleetcarAppService)
            : base(repository)
        {
            _fleetcarAppService = fleetcarAppService;
        }

        public override async Task<OrdersDto> CreateAsync(CreateUpdateOrdersDto input)
        {

            input.Status = true; // 自动设为已下单
            input.OrderNo = string.Format(DateTime.Now.ToString("yyyyMMddHHmmssfff")); // 时间戳生成单号

            var order = await base.CreateAsync(input);

            // 获取当前小时
            var hour = DateTime.Now.Hour;
            string quyu = hour < 12 ? "上午" : "下午";
            // 自动创建派车记录
            var fleetcar = new CreateUpdateFleetcarDto
            {
                OrderId = order.Id,
                // 其他字段可根据订单信息赋值
                Shipper = order.Shipper,
                ShipperAddress = order.ShipperAddress,
                Consignee = order.Consignee,
                ConsigneeAddress = order.ConsigneeAddress,
                Goods = order.Goods,
                PieceCount = order.PieceCount,
                Declaration = order.Declaration,
                CubicMeterss = order.CubicMeterss,
                Weightk = order.Weightk,
                Lengthm = order.Lengthm,
                Widthm = order.Widthm,
                Heightm = order.Heightm,
                FreightAmount = order.FreightAmount,
                ReturnRequired = order.ReturnRequired,
                Quyu = quyu, // 视业务而定
                CustomerNo = order.CustomerNo,
                OrderNo = order.OrderNo,
                Remarks = order.Remarks
            };
            await _fleetcarAppService.CreateAsync(fleetcar);

            return order;
        }
    }
} 